import datetime

import allure

from api import projectGroup
from api.utils import request
from case.BaseTestCase import BaseTestCase
from config import config


@allure.epic('筛选器模块')
@allure.feature('curd')
class FilterCurdCase(BaseTestCase):
    @allure.story('主流程')
    def test_filter_4_project_group_curd(self):
        """
        查找根项目组 Id
        """
        if_success, error, result1 = projectGroup.getRootProjectGroup()
        assert if_success, error

        root_project_group_id = result1['data'][0]['id']

        """
        保存过滤器
        """
        cur_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        filter_4_project_group_name = 'filter_for_project_group' + cur_time
        filter_4_project_group_uri = config['default_env_config']['url_prev'] + \
                                     'dashboard/v3/projects-performance/group/' + \
                                     root_project_group_id + \
                                     '/dev-equivalent?filterKey=c5e0bd5a78d6cbe2b144781bb1a534ce'

        if_success, error, result2 = request.request_api('filter/saveFilter', [filter_4_project_group_name,
                                                                               filter_4_project_group_uri,
                                                                               {"recentPeriod": {"counts": 3,
                                                                                                 "unit": "month"},
                                                                                "records": [{"type": "projectGroup",
                                                                                             "id": root_project_group_id}]}])

        saved_filter_id = result2['id']
        assert if_success, error
        self.assertEqual(filter_4_project_group_name, result2['name'])

        """
        保存第二个过滤器
        """
        cur_time_2 = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        filter2_4_project_group_name = 'filter2_for_project_group' + cur_time_2
        filter2_4_project_group_uri = config['default_env_config']['url_prev'] + \
                                      'dashboard/v3/projects-performance/group/' + \
                                      root_project_group_id + \
                                      '/dev-equivalent?filterKey=c5e0bd5a78d6cbe2b144781bb1a534ce'

        if_success, error, result3 = request.request_api('filter/saveFilter', [filter2_4_project_group_name,
                                                                               filter2_4_project_group_uri,
                                                                               {"recentPeriod": {"counts": 3,
                                                                                                 "unit": "month"},
                                                                                "records": [{"type": "projectGroup",
                                                                                             "id": root_project_group_id}]}])

        saved_filter2_id = result3['id']
        assert if_success, error

        """
        通过 id 获取筛选器
        """
        if_success, error, result4 = request.request_api('filter/getFilterById', [saved_filter_id])
        assert if_success, error
        self.assertEqual(saved_filter_id, result4['id'])
        self.assertEqual(filter_4_project_group_name, result4['name'])

        if_success, error, result5 = request.request_api('filter/getFilterById', [saved_filter2_id])
        assert if_success, error
        self.assertEqual(saved_filter2_id, result5['id'])
        self.assertEqual(filter2_4_project_group_name, result5['name'])

        """
        获取保存的筛选器
        """
        if_success, error, result6 = request.request_api('filter/getFilters', [{"pageSize": 10, "current": 1},
                                                                               {"sort": {"by": "create_time",
                                                                                         "order": "ASC"}}])
        assert if_success, error
        self.assertEqual(saved_filter_id, result6['results'][0]['id'])
        self.assertEqual(saved_filter2_id, result6['results'][1]['id'])

        """
        修改筛选器
        """
        updated_filter_4_project_group_name = 'filter_for_project_group' + cur_time + '_updated'
        if_success, error, result7 = request.request_api('filter/updateFilter', [saved_filter_id,
                                                                                 {"name": updated_filter_4_project_group_name}])
        assert if_success, error
        self.assertEqual(saved_filter_id, result7['id'])
        self.assertEqual(updated_filter_4_project_group_name, result7['name'])

        """
        删除筛选器
        """
        if_success, error, result8 = request.request_api('filter/deleteFilter', [saved_filter_id])
        assert if_success, error
        self.assertTrue(result8['is_delete'], 'Delete Filter Failed!')

        if_success, error, result9 = request.request_api('filter/deleteFilter', [saved_filter2_id])
        assert if_success, error
        self.assertTrue(result9['is_delete'], 'Delete Filter Failed!')